import java.util.Arrays;

class Student{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Test {
    public static void main1(String[] args) {
        //1使用常量串构造
        String str1 = "hello";
        //2直接new String对象
        String str2 = new String("abc");
        //3使用字符串数组进行构造
        char[] chars = {'a','s','d'};
        String str3 = new String(chars);

        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }

    public static void main11(String[] args) {
        int a = 10;
        int b = 20;
        int c = 10;
        //对于基本类型，==比较的是两个变量存储的值是否相同
        System.out.println(a == b); //false
        System.out.println(a == c); //true

        //对于引用类型，==比较的是两个引用变量引用的是否为同一个对象（比较地址）
        String s1 = new String("hello");
        String s2 = new String("hello");
        String s3 = new String("world");
        String s4 = s1;
        System.out.println(s1 == s2); //false
        System.out.println(s2 == s3); //false
        System.out.println(s1 == s4); //true
    }

    public static void main2(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("HEllo");
        String s3 = new String("hello");
        String s4 = s1;

        System.out.println(s3);
        System.out.println(s3.length()); //5
        System.out.println("hello".length()); //5

        System.out.println(s1.equals(s2)); //false 字符串比较是否相同用equals
        System.out.println(s1.equals(s3)); //true
        System.out.println(s1.equals(s4)); //true
        System.out.println(s1.equalsIgnoreCase(s2)); //true 忽略大小写进行比较是否相同
    }

    public static void main22(String[] args) {
        String s1 = new String("abc");
        String s2 = new String("ac");
        String s3 = new String("abc");
        String s4 = new String("abcde");
        System.out.println(s1.compareTo(s2)); //不同输出字符差-1
        System.out.println(s1.compareTo(s3)); //相同输出0
        System.out.println(s1.compareTo(s4)); //前3个字符完全相同，输出长度差-3
        //int compareToIgnoreCase(String str)方法：与compareTo方式相同，但是忽略大小写比较
        String s11 = new String("abc");
        String s22 = new String("ac");
        String s33 = new String("ABc");
        String s44 = new String("abcdef");
        System.out.println(s1.compareToIgnoreCase(s2)); //不同输出字符差-1
        System.out.println(s1.compareToIgnoreCase(s3)); //相同输出0
        System.out.println(s1.compareToIgnoreCase(s4)); //前3个字符完全相同，输出长度差-3
    }

    public static void main3(String[] args) {
        String s1 = new String("abcd");
        String s2 = new String("Abcd");
//        int ret = s1.compareTo(s2); //s1和s2比较大小
//        System.out.println(ret);

        int ret = s1.compareToIgnoreCase(s2); //忽略大小写进行比较

        if(ret > 0){
            System.out.println("s1 > s2");
        }else if(ret == 0){
            System.out.println("s1 == s2");
        }else {
            System.out.println("s1 < s2");
        }
    }

    public static void main4(String[] args) {
        String str = "ababcabcd";
        /*for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i); //charAt()返回i位置上的字符
            System.out.print(ch);
        }*/

        System.out.println(str.indexOf('c')); //从头开始找，遇到第一个该元素就结束
        System.out.println(str.indexOf('c',3)); //指定位置开始找，如果没有则返回-1
        System.out.println(str.indexOf("abc")); //返回字符串的起始位置
        System.out.println(str.indexOf("abc",4));//指定位置开始找，如果没有则返回-1
        System.out.println("================");
        System.out.println(str.lastIndexOf('c')); //从后往前找第一个元素 7
        System.out.println(str.lastIndexOf('c',4)); //4
        System.out.println(str.lastIndexOf("abc")); //5
        System.out.println(str.lastIndexOf("abc",3)); //2
    }

    //字符串的转换
    public static void main8(String[] args) {
        /*for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }*/
        String str = String.valueOf(123);
        System.out.println(str);

        String str2 = String.valueOf(true);
        System.out.println(str2);

        String s3 = String.valueOf(new Student("hhh",18));
        System.out.println(s3);

        System.out.println("==================");
        int val1 = Integer.parseInt("123");
        System.out.println(val1);

        double val2 = Double.parseDouble("12.12");
        System.out.println(val2);
    }

    //大小写转换
    public static void main6(String[] args) {
        String str = "hello";
        System.out.println(str.toUpperCase()); //小写转大写
        String str2 = "HHH";
        System.out.println(str2.toLowerCase()); //大写转小写

        System.out.println("==========");

        String s = "hello";
        char[] chars = s.toCharArray();  //字符串转数组
        for (int i = 0; i < chars.length; i++){
            System.out.print(chars[i]);
        }
        System.out.println(Arrays.toString(chars));
        String s2 = new String(chars); //数组转字符串

        String s1 = String.format("%d-%d-%d",2022,10,25); //格式化
        System.out.println(s);
    }

    public static void main7(String[] args) {
        String str1 = "ababcabcdabcde";
        String ret = str1.replace('a','p');
        System.out.println(ret); //

        /*String ret2 = str1.replace("ab","pk");
        System.out.println(ret2);*/

        /*String ret3 = str1.replaceAll("abc","pkrg");
        System.out.println(ret3);*/

        String ret4 = str1.replaceFirst("abc","lll");
        System.out.println(ret4);
    }

    //字符串拆分
    public static void main(String[] args) {
        String str = "zhangsan&lisi&wangwu&zhaoliu";
        String[] ret = str.split("&"); //按照&拆分
        String[] ret2 = str.split("&",2); //按照&拆分为2个元素

        System.out.println(Arrays.toString(ret));
        System.out.println(Arrays.toString(ret2));

        //拆分IP地址
        String str3 = "127.0.0.1";
        String[] ret3 = str3.split("\\."); //"."需要用转义字符\\
        for(String x : ret3){
            System.out.println(x);
        }
        String str4 = "127\\0\\0\\1";
        String[] ret4 = str4.split("\\\\"); //"."需要用转义字符\\
        for(String x : ret4){
            System.out.println(x);
        }
        String str5 = "127\\0\\0\\1";
        String[] ret5 = str5.split("/"); //"."需要用转义字符\\
        for(String x : ret5){
            System.out.println(x);
        }
        System.out.println("==========");

        //多次拆分
        String str6 = "name=zhangsan&age=18";
       /* String[] ret6 = str6.split("=|&");
        System.out.println(Arrays.toString(ret6));*/

        String[] ret6 = str6.split("&"); // 拆分为：name=zhangsan   age=18
        for (String x : ret6){
            String[] ss = x.split("="); //拆分为：name  zhangsan   age=18
            for (String s : ss){
                System.out.println(s);
            }
        }
    }

    //字符串截取
    public static void main9(String[] args) {
        String str = "helloworld";
        String ret = str.substring(1);
        String ret1 = str.substring(1,4);
        System.out.println(ret);
        System.out.println(ret1);
    }

    public static void main10(String[] args) {
        String str = "  hello hello   ";
        System.out.print(str.trim()); //trim()去除两边的空格
        System.out.println("ppppp");
    }

    public static void main12(String[] args) {
        String str = "alallal";
        System.out.println(str.endsWith("al"));
    }
}
